package com.songlanyun.modules.hxcategory.service.impl;

import cn.hutool.core.map.MapUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.songlanyun.common.exception.RRException;
import com.songlanyun.common.utils.PageUtils;
import com.songlanyun.common.utils.Query;
import com.songlanyun.common.validator.ValidatorUtils;
import com.songlanyun.common.validator.group.AddGroup;
import com.songlanyun.common.validator.group.UpdateGroup;
import com.songlanyun.modules.exception.GoodsExceptionEnum;
import com.songlanyun.modules.hxcategory.dao.HxCategoryDao;
import com.songlanyun.modules.hxcategory.entity.HxCategoryEntity;
import com.songlanyun.modules.hxcategory.service.HxCategoryEntityService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;


/**
 * 平台商品分类
 */
@Service("hxCategoryService")
public class HxCategoryEntityServiceImpl extends ServiceImpl<HxCategoryDao, HxCategoryEntity> implements HxCategoryEntityService {



    /**
     * 平台创建商品分类
     *
     * @param entity
     * @return
     */
    @Override
    @Transactional
    public HxCategoryEntity create(HxCategoryEntity entity) {
        ValidatorUtils.validateEntity(entity, AddGroup.class);
        validateNameExists(entity);
        this.baseMapper.insert(entity);
        return entity;
    }

    /**
     * 平台修改商品分类
     *
     * @param entity
     * @return
     */
    @Override
    @Transactional
    public HxCategoryEntity modify(HxCategoryEntity entity) {
        ValidatorUtils.validateEntity(entity, UpdateGroup.class);
        validateExists(entity.getId());
        validateNameExists(entity);
        this.baseMapper.updateById(entity);
        return this.getById(entity.getId());
    }

    /**
     * 平台删除商品分类
     *
     * @param id
     */
    @Override
    @Transactional
    public void remove(Long id) {
        validateRemoveSafe(id);
        this.removeById(id);
    }


    /**
     * 验证商品分类是否存在
     *
     * @param id
     */
    public void validateExists(Long id) {
        HxCategoryEntity entity = this.getById(id);
        if (entity == null) {
            throw new RRException(GoodsExceptionEnum.CATEGORY_NOT_EXISTS);
        }
    }

    /**
     * 首页一级分类推荐
     *
     * @return
     */
    @Override
    public List<HxCategoryEntity> homeRecommend(int limit) {
        return this.baseMapper.selectList(
                new LambdaQueryWrapper<HxCategoryEntity>()
                        .eq(HxCategoryEntity::getRecommend, true)
                        .last(String.format("limit %d",limit))
                        .orderByAsc(HxCategoryEntity::getOrderIndex)
        );
    }

    /**
     * 验证分类的名称不能重复
     *
     * @param entity
     */
    private void validateNameExists(HxCategoryEntity entity) {
        HxCategoryEntity target = this.baseMapper.getByName(entity.getName());
        if (target != null && !target.equals(entity)) {
            throw new RRException(GoodsExceptionEnum.CATEGORY_NAME_REPEAT);
        }
    }


    /**
     * 删除分组时的安全验证
     * 要删除的分组不存在不能删除
     * 下面还有子分组的不能删除
     *
     * @param id
     */
    private void validateRemoveSafe(Long id) {
        HxCategoryEntity entity = this.getById(id);
        if (entity == null) {
            throw new RRException(GoodsExceptionEnum.CATEGORY_NOT_EXISTS);
        }

    }


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String name = MapUtil.getStr(params, "name");
        IPage<HxCategoryEntity> page = this.page(
                new Query<HxCategoryEntity>().getPage(params),
                new LambdaQueryWrapper<HxCategoryEntity>()
                        .like(StringUtils.isNotBlank(name),HxCategoryEntity::getName,name)
                        .orderByAsc(HxCategoryEntity::getOrderIndex)
        );

        return new PageUtils(page);
    }



    /**
     * 查询所有商品分类
     *
     * @return
     */
    @Override
    public List<HxCategoryEntity> getAllCategory() {
        return  this.list(
                new LambdaQueryWrapper<HxCategoryEntity>()
                        .orderByAsc(HxCategoryEntity::getOrderIndex)
        );
    }


}

